package com.wish.group05.finance.controller;

import java.lang.ref.ReferenceQueue;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.group05.base.dto.ShowDealPerDayDateDto;
import com.wish.group05.base.entity.CwDealPerDay;
import com.wish.group05.base.entity.CwPaymentPlan;
import com.wish.group05.base.entity.CwReceiptPlan;
import com.wish.group05.base.entity.MTenant;
import com.wish.group05.base.service.ICwDealPerDayService;
import com.wish.group05.base.service.ICwPaymentPlanService;
import com.wish.group05.base.service.ICwReceiptPlanService;
import com.wish.group05.base.service.IMTenantService;
import com.wish.group05.base.vo.PaymentPlanVo;
import com.wish.group05.finance.dto.CwDealPerDayDto;
import com.wish.group05.finance.vo.CwDealPerDayTotalVo;
import com.wish.group05.finance.vo.ReceiptPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author wish
 * @date 2022-06-01
 */
@RestController
@RequestMapping("/day")
public class TCwDealPerDayController extends BaseController
{
    @Autowired
    ICwDealPerDayService cwDealPerDayService;
    @Autowired
    ICwReceiptPlanService cwReceiptPlanService;
    @Autowired
    IMTenantService imTenantService;
    @Autowired
    ICwPaymentPlanService cwPaymentPlanService;

    @GetMapping("getZhiChu")
    public AjaxResult getZhiChu(ShowDealPerDayDateDto showDealPerDayDateDto, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize) {
        if (showDealPerDayDateDto.getRq() != null) {
            Date dealDate = getDateAfterDays(showDealPerDayDateDto.getDealDate(), showDealPerDayDateDto.getRq());
            showDealPerDayDateDto.setDealDate(dealDate);
        }
        QueryWrapper<CwPaymentPlan> wrapper = new QueryWrapper<>();
        if (showDealPerDayDateDto.getDealDate() != null) {
            wrapper.eq("paid_time",showDealPerDayDateDto.getDealDate());
        }
        //删除状态为未删除的
        wrapper.eq("del_flg",0);
        //已收款的
        wrapper.eq("pay_state",2);
        PageHelper.startPage(pageNum,pageSize);
        //查询收款表
        List<CwPaymentPlan> list = cwPaymentPlanService.list(wrapper);
        PageInfo<CwPaymentPlan> pg = new PageInfo<>(list);


        //获取统计金额
        PaymentPlanVo planVo = cwDealPerDayService.searchCwDealPerDayDetailPaySum(showDealPerDayDateDto);
        planVo.setDealDate(showDealPerDayDateDto.getDealDate());
        planVo.setPg(pg);
        return AjaxResult.success();
    }

    /**
     *
     * @Description: 收入    收款明细分页查询
     * @param @param request
     * @param @return 参数
     * @return 返回值说明
     * 收入    收入金额总计
     */
    @GetMapping("getShow")
    public AjaxResult getShow(ShowDealPerDayDateDto showDealPerDayDateDto, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize) {

        ReceiptPlanVo receiptPlanVo = new ReceiptPlanVo();
        if (showDealPerDayDateDto.getRq() != null) {
            Date dealDate = getDateAfterDays(showDealPerDayDateDto.getDealDate(), showDealPerDayDateDto.getRq());
            showDealPerDayDateDto.setDealDate(dealDate);
        }
        QueryWrapper<CwReceiptPlan> wrapper = new QueryWrapper<>();
        if (showDealPerDayDateDto.getDealDate() != null) {
            wrapper.eq("real_collection_date",showDealPerDayDateDto.getDealDate());
        }
        //删除状态为未删除的
        wrapper.eq("del_flg",0);
        //已收款的
        wrapper.eq("sk_status",2);
        wrapper.eq("bill_type",3).or().eq("bill_type",4);
        PageHelper.startPage(pageNum,pageSize);
        //查询收款表
        List<CwReceiptPlan> list = cwReceiptPlanService.list(wrapper);
        BigDecimal sum = new BigDecimal(0);
        for (CwReceiptPlan cwReceiptPlan : list) {
            if (cwReceiptPlan.getRealPaymentAmount() != null && cwReceiptPlan!=null) {
                sum=sum.add(cwReceiptPlan.getRealPaymentAmount());
            }
        }

        //查询租客表
        List<MTenant> mTenantList = imTenantService.list();
        receiptPlanVo.setDealDate(showDealPerDayDateDto.getDealDate());
        receiptPlanVo.setSumTotal(sum);
        PageInfo<CwReceiptPlan> pg =new PageInfo<>(list);
        receiptPlanVo.setPg(pg);
        receiptPlanVo.setMTenantList(mTenantList);
        return AjaxResult.success(receiptPlanVo);
    }

    public Date getDateAfterDays(Date startDate, int afterDays) {
        if (null == startDate) {
            return null;
        }
        Calendar calendar =Calendar.getInstance();
        // 起点日期
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE, afterDays);

        return calendar.getTime();
    }

    /**
     * 获取各个地方的统计金钱
     * @param cwDealPerDayDto
     * @return
     */
    @GetMapping("getMoneyStatistics")
    public AjaxResult getMoneyStatistics(CwDealPerDayDto cwDealPerDayDto) {
        QueryWrapper<CwDealPerDay> wrapper = new QueryWrapper<>();
        if (cwDealPerDayDto.getDealDate() != null) {
            wrapper.ge("deal_date",cwDealPerDayDto.getDealDate());
        }
        if (cwDealPerDayDto.getDealDate1() != null) {
            wrapper.le("deal_date",cwDealPerDayDto.getDealDate1());
        }
        wrapper.eq("del_flg",0);
        wrapper.eq("operation_type",cwDealPerDayDto.getOperationType());
        List<CwDealPerDay> list = cwDealPerDayService.list(wrapper);

        BigDecimal totalSum =new BigDecimal(0);
        BigDecimal depositSum=new BigDecimal(0);
        BigDecimal rentSum=new BigDecimal(0);
        BigDecimal electricityFeeSum=new BigDecimal(0);
        BigDecimal waterFeeSum=new BigDecimal(0);
        BigDecimal netFeeSum=new BigDecimal(0);
        BigDecimal gasFeeSum=new BigDecimal(0);
        BigDecimal cableFeeSum=new BigDecimal(0);
        BigDecimal otherFeeSum=new BigDecimal(0);
        CwDealPerDayTotalVo cwDealPerDayTotalVo = new CwDealPerDayTotalVo();
        for (CwDealPerDay cwDealPerDay : list) {
            totalSum =totalSum.add(cwDealPerDay.getTotal()==null?new BigDecimal(0):cwDealPerDay.getTotal());
            depositSum=depositSum.add(cwDealPerDay.getDeposit()==null?new BigDecimal(0):cwDealPerDay.getDeposit());
            rentSum=rentSum.add(cwDealPerDay.getRent()==null?new BigDecimal(0):cwDealPerDay.getRent());
            electricityFeeSum=electricityFeeSum.add(cwDealPerDay.getElectricityFee()==null?new BigDecimal(0):cwDealPerDay.getElectricityFee());
            waterFeeSum=waterFeeSum.add(cwDealPerDay.getWaterFee()==null?new BigDecimal(0):cwDealPerDay.getWaterFee());
            netFeeSum=netFeeSum.add(cwDealPerDay.getNetFee()==null?new BigDecimal(0):cwDealPerDay.getNetFee());
            gasFeeSum=gasFeeSum.add(cwDealPerDay.getGasFee()==null?new BigDecimal(0):cwDealPerDay.getGasFee());
            cableFeeSum=cableFeeSum.add(cwDealPerDay.getCableFee()==null?new BigDecimal(0):cwDealPerDay.getCableFee());
            otherFeeSum=otherFeeSum.add(cwDealPerDay.getOtherFee()==null?new BigDecimal(0):cwDealPerDay.getOtherFee());
        }
        cwDealPerDayTotalVo.setTotalSum(totalSum);
        cwDealPerDayTotalVo.setCableFeeSum(cableFeeSum);
        cwDealPerDayTotalVo.setDepositSum(depositSum);
        cwDealPerDayTotalVo.setElectricityFeeSum(electricityFeeSum);
        cwDealPerDayTotalVo.setGasFeeSum(gasFeeSum);
        cwDealPerDayTotalVo.setNetFeeSum(netFeeSum);
        cwDealPerDayTotalVo.setOtherFeeSum(otherFeeSum);
        cwDealPerDayTotalVo.setRentSum(rentSum);
        cwDealPerDayTotalVo.setWaterFeeSum(waterFeeSum);
        return AjaxResult.success(cwDealPerDayTotalVo);
    }

    /**
     * 查询营业列表列表
     */
    @PreAuthorize(hasPermi = "finance:day:daylist")
    @GetMapping("/list")
    public TableDataInfo list(CwDealPerDayDto cwDealPerDayDto)
    {
        QueryWrapper<CwDealPerDay> wrapper = new QueryWrapper<>();
        if (cwDealPerDayDto.getDealDate() != null) {
            wrapper.ge("deal_date",cwDealPerDayDto.getDealDate());
        }
        if (cwDealPerDayDto.getDealDate1() != null) {
            wrapper.le("deal_date",cwDealPerDayDto.getDealDate1());
        }
        wrapper.eq("del_flg",0);
        wrapper.eq("operation_type",cwDealPerDayDto.getOperationType());

        startPage();
        List<CwDealPerDay> list = cwDealPerDayService.list(wrapper);
        return getDataTable(list);
    }

}
